<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <MyHeader :addTodo="addTodo"/>
        <MyList :todos="todos" :checkTodo="checkTodo" :deleteTodo="deleteTodo"/>
        <MyFooter :todos="todos" :checkAllTodos="checkAllTodos" :chearAllTodos="chearAllTodos"/>
      </div>
    </div>
  </div>
  
</template>

<script>
import MyHeader from './components/MyHeader.vue'
import MyFooter from './components/MyFooter.vue'
import MyList from './components/MyList.vue'
import axios from 'axios';
export default {
    name: 'App',
      components: {
        MyFooter, MyList, MyHeader
      },
    data(){
          return{
              todos:[]
          }
      },
    created(){
        const showURL = "http://47.100.196.100:8080/item/showAll"
        axios.get(showURL)
        .then(response => {
           this.todos = response.data
        })
    },
    methods:{
      
      // 添加一个todo
      addTodo(todo){
        const addURL = "http://47.100.196.100:8080/item/add"
        axios.post(addURL, todo)
        .then(response => {
           this.todos = response.data
        })
      },
      // 勾选 or 取消勾选
      checkTodo(done, id){
        const changeURL = "http://47.100.196.100:8080/item/selectById"
        axios.get(changeURL,{params: {'done': done, 'id': id}})
        .then(response => {
           this.todos = response.data
        })
      },
      // 删除一个todo
      deleteTodo(id){
        const delURL = "http://47.100.196.100:8080/item/del"
        axios.delete(`${delURL}/${id}`)
        .then(response => {
           this.todos = response.data
        })
      },
      // 全选 与 全不选
      checkAllTodos(done){
        const selectURL = "http://47.100.196.100:8080/item/selectAll"
        axios.get(selectURL, {params: {'done': done}})
        .then(response => {
           this.todos = response.data
        })
      },
      // 清楚所有已经完成的元素
      chearAllTodos(){
        if(confirm("确定清除所有已完成吗?")){
          const delAllURL = "http://47.100.196.100:8080/item/delAll"
          axios.delete(delAllURL)
          .then(response => {
              this.todos = response.data
          })
        }
      }
    }
} 
</script>

<style>
  body{
    background-color: #fff;
  }
  .btn{
    display: inline-block;
    padding: 4px 12px;
    margin-bottom: 0;
    font-size: 14px;
    line-height: 20px;
    text-align: center;
    vertical-align: middle;
    cursor: pointer;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
    border-radius: 4px;
  }
  .btn-danger{
    color: #fff;
    background-color: #de4f49;
    border: 1px solid #bd362f;
  }
  .btn-danger:hover{
    color: #fff;
    background-color: #bd362f;
  }
  .btn:focus{
    outline: none;
  }

  .todo-container{
    width: 600px;
    margin: 0 auto;
  }
  .todo-container .todo-wrap{
    padding: 10px;
    border: 1px solid #ddd;
    border-radius: 5px;

  }
</style>
